#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
using namespace std;

void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    glViewport(0, 0, width, height);
}

int main()
{
    // glad加载
    if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "failed to init glad!" << std::endl;
    }

    // 告知opengl我们使用的版本和渲染模式
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // mac 下需要这句话
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    // end

    // 创建opengl窗口
    GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }

    // 创建上下文
    glfwMakeContextCurrent(window);
    // 渲染窗口自适应
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    // 等待用户关闭窗口
    while(!glfwWindowShouldClose(window))
    {
        // 双缓冲交换
        glfwSwapBuffers(window);
        // 响应各种交互事件
        glfwPollEvents();
    }
    // 释放资源
    glfwTerminate();
    return 0;
}
